package com.zlkj.pro.modular.business.controller;

import cn.stylefeng.roses.kernel.auth.api.context.LoginContext;
import cn.stylefeng.roses.kernel.rule.enums.YesOrNotEnum;
import cn.stylefeng.roses.kernel.rule.pojo.response.ResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.SuccessResponseData;
import cn.stylefeng.roses.kernel.scanner.api.annotation.ApiResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.GetResource;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zlkj.pro.modular.admin.entity.Config;
import com.zlkj.pro.modular.admin.entity.User;
import com.zlkj.pro.modular.admin.mapper.UserMapper;
import com.zlkj.pro.modular.admin.model.dto.DashboardDTO;
import com.zlkj.pro.modular.admin.model.dto.UserDTO;
import com.zlkj.pro.modular.admin.model.vo.UserVO;
import com.zlkj.pro.modular.business.model.dto.AgentDTO;
import com.zlkj.pro.modular.business.model.vo.AgentVO;
import com.zlkj.pro.modular.business.service.DashboardService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import static com.zlkj.pro.core.consts.ProjectConstants.SERVICE_API_PREFIX;

/**
 * @author liyang
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@ApiResource(name = "报表信息", path = SERVICE_API_PREFIX + "/dashboard")
public class DashboardController {

    private final DashboardService dashboardService;

    private final UserMapper userMapper;

    @GetResource(name = "获取用户信息", path = "/userInfo")
    public ResponseData userInfo(UserDTO dto) {
        Long userId = LoginContext.me().getLoginUser().getUserId();
        UserVO user = dashboardService.selectUserInfo(userId);
        return new SuccessResponseData(user);
    }

    @GetResource(name = "客户分析数据", path = "/customer")
    public ResponseData customer(DashboardDTO dto) {
        Long userId = LoginContext.me().getLoginUser().getUserId();
        dto.setUserId(userId);
        Map<String, Object> restData = dashboardService.customer(dto);
        return new SuccessResponseData(restData);
    }

    @GetResource(name = "综合统计数据", path = "/customerByMonths")
    public ResponseData customerByMonths(DashboardDTO dto) {
        Long userId = LoginContext.me().getLoginUser().getUserId();
        dto.setUserId(userId);
        List<Map<String, Object>> restData = dashboardService.customerByMonths(dto);
        return new SuccessResponseData(restData);
    }


    @GetResource(name = "销售业绩", path = "/salesPerformance")
    public ResponseData salesPerformance(DashboardDTO dto) {
        Long userId = LoginContext.me().getLoginUser().getUserId();
        dto.setUserId(userId);
        List<Map<String, Object>> restData = dashboardService.salesPerformance(dto);
        return new SuccessResponseData(restData);
    }

    @GetResource(name = "销售排行", path = "/seniorityUser")
    public ResponseData seniorityUser(DashboardDTO dto) {
        Long userId = LoginContext.me().getLoginUser().getUserId();
        dto.setUserId(userId);
        List<Map<String, Object>> restData = dashboardService.seniorityUser(dto);
        return new SuccessResponseData(restData);
    }

    @GetResource(name = "代理商排行", path = "/seniorityAgent")
    public ResponseData seniorityAgent(DashboardDTO dto) {
        Long userId = LoginContext.me().getLoginUser().getUserId();
        dto.setUserId(userId);
        List<Map<String, Object>> restData = dashboardService.seniorityAgent(dto);
        return new SuccessResponseData(restData);
    }

    @GetResource(name = "地域排行", path = "/seniorityRegion")
    public ResponseData seniorityRegion(DashboardDTO dto) {
        Long userId = LoginContext.me().getLoginUser().getUserId();
        dto.setUserId(userId);
        List<Map<String, Object>> restData = dashboardService.seniorityRegion(dto);
        return new SuccessResponseData(restData);
    }


    @GetResource(name = "查询代理商列表", path = "/findAgentList")
    public ResponseData findAgentList(AgentDTO dto) {
        List<AgentVO> restData = dashboardService.findAgentList(dto);
        return new SuccessResponseData(restData);
    }


    @GetResource(name = "获取地域信息", path = "/getRegions")
    public ResponseData getRegions() {
        Long userId = LoginContext.me().getLoginUser().getUserId();
        User user = userMapper.selectById(userId);
        if (StringUtils.isNotBlank(user.getRegion())) {
            List<String> regions = Arrays.asList(user.getRegion().split(","));
            return new SuccessResponseData(regions);
        } else {
            return new SuccessResponseData(new ArrayList<>());
        }
    }

}
